package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.me.adapter.MyAccountAdapter;
import com.uchengwang.ui.cm_ui.umsg.bean.ClubListBean;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.cm_ui.umsg.ui.ClubAuthenActivity;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.PopupUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/6/8.
 * 我的社团
 */

public class MyAccountActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private RelativeLayout rl;
    private RecyclerView rv;
    private MyAccountAdapter adapter;
    private List<SchoolClubBean> lists ;

    private List<String> stringList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_clubs);
        setTitle("账号管理");

        stringList.add("社团号");
        stringList.add("服务号");

        setRightText("申请", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupUtils.getUtils().rgihtMore(MyAccountActivity.this, getRy(), stringList, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        PopupUtils.getUtils().dismiss();
                        Intent intent = null;
                        switch (position){
                            case 0:
                                if (UCommonUtil.getUtil().isNeedLogin(MyAccountActivity.this, false)){
                                    return;
                                }
                                intent = new Intent(MyAccountActivity.this, ClubAuthenActivity.class);
                                break;
                            case 1:
                                UCommonUtil.getUtil().showToast(MyAccountActivity.this, "开通中...");
                                break;
                        }

                        if (intent != null){
                            startActivity(intent);
                        }
                    }
                });
            }
        });
        initView();

    }

    private void initView(){
        rl = (RelativeLayout) findViewById(R.id.myAccount_rl);
        rl.setOnClickListener(this);
        rv = (RecyclerView) findViewById(R.id.myAccount_rv);

        rv.setLayoutManager(new LinearLayoutManager(MyAccountActivity.this));
        adapter = new MyAccountAdapter(this);
        rv.setAdapter(adapter);

        getMyClubs();
    }

    /**
     * 获取本人创建的所有社团
     * */
    private void getMyClubs(){
        LoadingUtils.getUtils().loading(this);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, 1);
        map.put(BaseConfig.PERPAGE, 20);
        HttpManager.newInstance(this).query(UserConfig.MyClubs.method, UserConfig.MyClubs.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                UCommonUtil.getUtil().showToast(MyAccountActivity.this, "获取失败");

                getMyJoinClubs();
            }

            @Override
            public void onResp(int status, String msg, String data) {

                Log.d("jjf", "status="+status+"=msg="+msg+"=data="+data);
                if (status == 0 && !TextUtils.isEmpty(data)){
                    ClubListBean bean = JsonUtils.getManager().stringToBean(data, ClubListBean.class);
                    lists = bean.getData();

                    if (lists !=null && lists.size() >0){
                        for (SchoolClubBean b : lists){
                            b.setMyCreate(true);
                        }
                    }
                }

                //再去获取我加入的社团
                getMyJoinClubs();

            }
        });
    }

    /**
     * 获取本人加入的所有社团
     * */
    private void getMyJoinClubs(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, 1);
        map.put(BaseConfig.PERPAGE, 20);
        HttpManager.newInstance(this).query(UserConfig.JoinClub.method, UserConfig.JoinClub.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "status="+status+"=msg="+msg);
                UCommonUtil.getUtil().showToast(MyAccountActivity.this, "获取失败");

                if (lists != null && lists.size() >0){
                    adapter.setData(lists);
                }
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();

                Log.d("jjf", "status="+status+"=msg="+msg+"=data="+data);
                if (status != 0 || TextUtils.isEmpty(data) || data.equals("[]")){
                    if (lists != null && lists.size() >0){
                        adapter.setData(lists);
                    }
                    return;
                }

                ClubListBean bean = JsonUtils.getManager().stringToBean(data, ClubListBean.class);
                List<SchoolClubBean> ll = bean.getData();
                if (ll != null && ll.size() >0){
                    if (lists ==null){
                        lists = new ArrayList<SchoolClubBean>();
                    }
                    lists.addAll(ll);
                    adapter.setData(lists);
                }

            }
        });
    }


    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.myAccount_rl://社团认证
                if (UCommonUtil.getUtil().isNeedLogin(this, false)){
                    return;
                }
                intent = new Intent(this, ClubAuthenActivity.class);
                break;
        }
        if (intent != null){
            startActivity(intent);
        }
    }
}
